package com.margo.project.socket;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@RequestMapping(value = "/iep/mobile/mobileservice/fileAction")
@Controller
public class FileAction {
	@RequestMapping(method = RequestMethod.POST, value = "/upByteFile.iep")
	@ResponseBody
	public Object upByteFile(HttpServletRequest request, HttpServletResponse response) {
		InputStream inputStream=null;
		FileOutputStream fos=null;
		try {
			inputStream = request.getInputStream();
			//这种获取的文件写不进去
			URI uri = this.getClass().getResource("/file/socket.txt").toURI();
			File file=new File(uri);
			//获取绝对路径在编译后的文件里面
			System.out.println(file.getAbsolutePath());
			//可以写
			System.out.println(file.canWrite());
			fos=new FileOutputStream(file);
			byte[] t=new byte[1024];
			int len=0;
			while((len=inputStream.read(t))!=-1) {
				fos.write(t, 0, len);
			}
			fos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(fos !=null) {
					fos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return "ok";
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/upCharFile.iep")
	@ResponseBody
	public Object upCharFile(HttpServletRequest request, HttpServletResponse response) {
		InputStream inputStream=null;
		FileOutputStream fos=null;
		InputStreamReader isr=null;
		OutputStreamWriter osw=null;
		try {
			inputStream = request.getInputStream();
			isr=new InputStreamReader(inputStream, Charset.forName("utf-8"));
			URI uri = this.getClass().getResource("/file/socket.txt").toURI();
			File file=new File(uri);
			fos=new FileOutputStream(file);
			osw=new OutputStreamWriter(fos, "utf-8");
			char[] t=new char[2];
			int len=0;
			while((len=isr.read(t))!=-1) {
				osw.write(t, 0, len);
			}
			osw.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(fos !=null) {
					fos.close();
				}
				if(isr !=null) {
					fos.close();
				}
				if(osw !=null) {
					fos.close();
				}
		
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return "ok";
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/upCharString.iep")
	@ResponseBody
	public Object upCharString(HttpServletRequest request, HttpServletResponse response) {
		InputStream inputStream=null;
		FileOutputStream fos=null;
		InputStreamReader isr=null;
		OutputStreamWriter osw=null;
		try {
			inputStream = request.getInputStream();
			isr=new InputStreamReader(inputStream, "utf-8");
			URI uri = this.getClass().getResource("/file/socket.txt").toURI();
			File file=new File(uri);
			fos=new FileOutputStream(file);
			osw=new OutputStreamWriter(fos, "utf-8");
			char[] t=new char[2];
			int len=0;
			while((len=isr.read(t))!=-1) {
				System.out.println(t);
				osw.write(t, 0, len);
			}
			osw.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(fos !=null) {
					fos.close();
				}
				if(isr !=null) {
					fos.close();
				}
				if(osw !=null) {
					fos.close();
				}
		
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return "ok";
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/multipartFile.iep")
    @ResponseBody
    public String multipartFile(@RequestParam("file") MultipartFile file) throws URISyntaxException, IOException {
        if (file.isEmpty()) {
            return "file is null";
        }        
    	URI uri = this.getClass().getResource("/file/socket2.txt").toURI();
		File dest=new File(uri);
        try {
            file.transferTo(dest);
            return "ok";
        } catch (IOException e) {
        	e.printStackTrace();
        }
        return "false";
    }

}
